package com.school.controller;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.school.entity.HljtClass;
import com.school.entity.HljtClassimage;
import com.school.entity.HljtClassnotify;
import com.school.entity.HljtClassschedulecard;
import com.school.entity.HljtDevice;
import com.school.entity.HljtGradetime;
import com.school.entity.HljtStudent;
import com.school.entity.HljtTeacher;
import com.school.entity.HljtTeacherclass;
import com.school.entity.android.Curriculum;
import com.school.entity.android.Images;

import com.school.entity.android.ClassCurriculum;
import com.school.entity.android.ClassHomePage;
import com.school.util.JsonInterface;


import com.school.util.WhetherNotToUpdate;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.school.service.impl.HljtDeviceServiceImpl;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtClassimageServiceImpl;
import com.school.service.impl.HljtClassnotifyServiceImpl;
import com.school.service.impl.HljtClassschedulecardServiceImpl;
import com.school.service.impl.HljtGradetimeServiceImpl;
import com.school.service.impl.HljtStudentServiceImpl;
import com.school.service.impl.HljtTeacherclassServiceImpl;
import com.school.service.impl.HljtTeacherServiceImpl;

@Controller
@RequestMapping("/HomePageController")
public class HomePageController {
	
	@Autowired
	private HljtDeviceServiceImpl hljtDeviceServiceImpl;
	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	@Autowired
	private HljtClassschedulecardServiceImpl hljtClassschedulecardServiceImpl;
	@Autowired
	private HljtGradetimeServiceImpl hljtGradetimeServiceImpl;
	@Autowired
	private HljtTeacherclassServiceImpl hljtTeacherclassServiceImpl;
	@Autowired
	private HljtTeacherServiceImpl hljtTeacherServiceImpl;
	@Autowired
	private HljtClassnotifyServiceImpl hljtClassnotifyServiceImpl;
	@Autowired
	private HljtStudentServiceImpl hljtStudentServiceImpl;
	@Autowired
	private HljtClassimageServiceImpl hljtClassimageServiceImpl;
	
	Integer classId;
	
	HljtClass hljtClass=new HljtClass();
	
	/**
	 * 安卓端首页信息
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/selectPage")
	@ResponseBody
	public JsonInterface selectPage(String mac) throws Exception {
		JsonInterface  jsonInterface=new JsonInterface();
		
		JSONObject data=new JSONObject();
		List<Curriculum> Curriculumlist=new ArrayList<>();
		if(mac !=null) {
			HljtDevice  HljtDevice=hljtDeviceServiceImpl.findDevice(mac);
			//获取班级id
			if(HljtDevice !=null) {
				classId=HljtDevice.getClassid();
				if(null!=classId) {
					//通过班级id查找对应班级信息
					hljtClass =hljtClassServiceIpml.findClass(classId);
					/**	//List<HljtClassimage>  ll=hljtClassimageServiceImpl.findListImage(classId, "5",null);*/
					//获取班级名字
					String className=hljtClass.getGrade()+"("+hljtClass.getClasses()+")";
					//获取班级对应课程
					Map<String, Object>  Curriculumlista= this.getCurriculum();
				    //获取班主任  班级之星  班级荣誉  班级通知
					ClassHomePage classHomePage=this.getClassHomePage();
					//获取学生作品
					List<Images> works =this.getworks();
				
					List<Images> classStyle =this.getclassStyle();

					ClassCurriculum ClassCurriculum=this.selectCurriculum();
					
					JSONObject arrays = JSONObject.fromObject(classHomePage);
					JSONArray array1=JSONArray.fromObject(works);
					JSONArray array2=JSONArray.fromObject(classStyle);
					JSONObject array3 = JSONObject.fromObject(ClassCurriculum);
					
					data.put("works", array1);
					data.put("classStyle", array2);
					data.put("banzhuren", arrays);
					//if(null!=Curriculumlista&&Curriculumlista.size()>0) {
					//	JSONArray array=JSONArray.fromObject(Curriculumlista);
						data.put("kechengbiao", Curriculumlista);
//					}else {
//						List<Curriculum> lista=new ArrayList<>();
//						JSONArray array=JSONArray.fromObject(lista);
//						data.put("kechengbiao", array);
//					}
					data.put("classname", className);
					data.put("kechengbiaoall", array3);
					
					
					jsonInterface.setData(data.toString());
					jsonInterface.setErrCode(0);
					jsonInterface.setErrMsg("请求成功");
				}else {
					jsonInterface.setData(null);
					jsonInterface.setErrCode(0);
					jsonInterface.setErrMsg("数据有误");
				}
			}else {
				jsonInterface.setData(null);
				jsonInterface.setErrCode(0);
				jsonInterface.setErrMsg("数据有误");
			}
		}else {
			jsonInterface.setData(null);
			jsonInterface.setErrCode(5);
			jsonInterface.setErrMsg("请求失败,设备未注册");

		}
	
		
 		return jsonInterface;
}
	/**
	 * 获取学生作品
	 * @return
	 * @throws Exception 
	 */
	public List<Images> getworks() throws Exception {
		
		String newIp= WhetherNotToUpdate.getIp();
		
		List<Images> listworks=new ArrayList<>();
		List<HljtClassimage>  HljtClassimage=hljtClassimageServiceImpl.findListImage(classId, "5",null);

		if(null !=HljtClassimage && HljtClassimage.size()>0) {
			for(int i=0;i<HljtClassimage.size();i++) {
				Images Images=new Images();
				Images.setTitle(HljtClassimage.get(i).getTitle());
				Images.setPictureUrl(newIp+HljtClassimage.get(i).getImagepath());
				Images.setContent(HljtClassimage.get(i).getContent());
				listworks.add(Images);
			}
		}else {
			Images Images=new Images();
			Images.setTitle("");
			Images.setPictureUrl("");
			Images.setContent("");
			listworks.add(Images);

		}
		return listworks;
	}
	/**
	 * 获取班级风采
	 * @return
	 * @throws Exception 
	 */
	public List<Images> getclassStyle() throws Exception {
		String newIp= WhetherNotToUpdate.getIp();
		
		List<Images> listworks=new ArrayList<>();
		List<HljtClassimage>  l=hljtClassimageServiceImpl.findListImage(classId, "4",null);

		if(null !=l && l.size()>0) {
			for(int i=0;i<l.size();i++) {
				Images Images=new Images();
				Images.setTitle(l.get(i).getTitle());
				Images.setPictureUrl(newIp+l.get(i).getImagepath());
				Images.setContent(l.get(i).getTitle());
				listworks.add(Images);
			}
		}else {
			Images Images=new Images();
			Images.setTitle("");
			Images.setPictureUrl("");
			Images.setContent("");
			listworks.add(Images);

		}
		return listworks;
	}
	
	public ClassHomePage getClassHomePage() throws Exception {
		
		String newIp= WhetherNotToUpdate.getIp();
		
		HljtTeacherclass hljtTeacher=hljtTeacherclassServiceImpl.findTeacherInformation(classId, 0);
		//获取班级之星id
		
		HljtStudent Student=hljtStudentServiceImpl.findStudent(hljtClass.getStudentid());
		//获取老师id查找对应老师详细信息
		HljtTeacher Teacher=new HljtTeacher();
		ClassHomePage ClassHomePage=new ClassHomePage();
		if(null!=hljtTeacher) {
			Teacher=hljtTeacherServiceImpl.findTeacher(hljtTeacher.getTeacherid());
			//设置老师图片
			ClassHomePage.setTeacherImage(newIp+Teacher.getImages());
			//设置老师名字
			ClassHomePage.setTeacherName(Teacher.getName());
			//科目
			ClassHomePage.setSubject(Teacher.getSubject());
			//荣誉
			ClassHomePage.setTeacherHonor(Teacher.getHonor());
			//寄语
			ClassHomePage.setTeacherMessage(Teacher.getLeaving());
		}else {
			//设置老师图片
			ClassHomePage.setTeacherImage(null);
			//设置老师名字
			ClassHomePage.setTeacherName(null);
			//科目
			ClassHomePage.setSubject(null);
			//荣誉
			ClassHomePage.setTeacherHonor(null);
			//寄语
			ClassHomePage.setTeacherMessage(null);
		}
		
		//学生照片
		if(null !=Student) {
			ClassHomePage.setStudentImage(newIp+Student.getStudentimage());
			//学生名字
			ClassHomePage.setStudentName(Student.getName());
			//学生评价
			ClassHomePage.setStrdentEvaluate(Student.getEvaluate());
			
		}else {
			ClassHomePage.setStudentImage("");
			//学生名字
			ClassHomePage.setStudentName("");
			//学生评价
			ClassHomePage.setStrdentEvaluate("");
		}
		List<HljtClassimage> HljtClassimage=hljtClassimageServiceImpl.findListImage(classId, "3", null);
		if(null!=HljtClassimage&&HljtClassimage.size()>0) {
			//设置班级荣誉
			ClassHomePage.setContent(HljtClassimage.get(HljtClassimage.size()-1).getContent());
			//设置班级荣誉
			ClassHomePage.setTitle(HljtClassimage.get(HljtClassimage.size()-1).getTitle());
			//设置班级荣誉
			ClassHomePage.setImagepath(newIp+HljtClassimage.get(HljtClassimage.size()-1).getImagepath());
		}else {
			//设置班级荣誉
			ClassHomePage.setContent(null);
			//设置班级荣誉
			ClassHomePage.setTitle(null);
			//设置班级荣誉
			ClassHomePage.setImagepath(null);
		}
		
		List<HljtClassnotify> HljtClassnotify =hljtClassnotifyServiceImpl.findNotity(classId, "2");
		
		if(null!=HljtClassnotify&&HljtClassnotify.size()>0) {
				//设置班级通知名字
				ClassHomePage.setClassNoticeTitle(HljtClassnotify.get(HljtClassnotify.size()-1).getTitle());
				//设置班级通知内容
				ClassHomePage.setClassNoticeContent(HljtClassnotify.get(HljtClassnotify.size()-1).getContent());
		}else {
			//设置班级通知名字
			ClassHomePage.setClassNoticeTitle(null);
			//设置班级通知内容
			ClassHomePage.setClassNoticeContent(null);
		}
		
		
		
		
		return ClassHomePage;
	}
		public ClassCurriculum selectCurriculum() {
		
		List<HljtClassschedulecard> hljtClassschedulecard =hljtClassschedulecardServiceImpl.findClassschedulecard(classId);
		
		ClassCurriculum ClassCurriculum=new ClassCurriculum();
		List<String> listmondy=new ArrayList<>();
		List<String> listtuesday=new ArrayList<>();
		List<String> listwednesday=new ArrayList<>();
		List<String> listthursday=new ArrayList<>();
		List<String> listfriday=new ArrayList<>();
		List<String> listsaturday=new ArrayList<>();
		List<String> listsunday=new ArrayList<>();
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listmondy.add(hljtClassschedulecard.get(i).getMonday());
		}
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listtuesday.add(hljtClassschedulecard.get(i).getTuesday());
		}
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listwednesday.add(hljtClassschedulecard.get(i).getWednesday());
		}
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listthursday.add(hljtClassschedulecard.get(i).getThursday());
		}
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listfriday.add(hljtClassschedulecard.get(i).getFriday());
		}
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listsaturday.add(hljtClassschedulecard.get(i).getSaturday());
		}
		for(int i=0;i<hljtClassschedulecard.size();i++) {
			listsunday.add(hljtClassschedulecard.get(i).getSunday());
		}
		ClassCurriculum.setMonday(listmondy);
		ClassCurriculum.setTuesday((listtuesday));
		ClassCurriculum.setWednesday((listwednesday));
		ClassCurriculum.setThursday(listthursday);
		ClassCurriculum.setFriday((listfriday));
		ClassCurriculum.setSaturday(listsaturday);
		ClassCurriculum.setSunday(listsunday);
		return ClassCurriculum;
	}
	
	/**
	 * 获取班级对应课程
	 * @return
	 */
	public Map<String, Object> getCurriculum(){
		
	//	List<Object> listObject=new ArrayList<>();
		Map<String, Object> mapCurriculum=new HashMap<>();
		
		//通过班级id查找班级课程表
		List<HljtClassschedulecard> HljtClassschedulecard =hljtClassschedulecardServiceImpl.findClassschedulecard(classId);
		//通过年级id查找对应年级上课时间
		List<HljtGradetime> HljtGradetime=hljtGradetimeServiceImpl.findGradetime(hljtClass.getGradeid());
		
		if(null!=HljtClassschedulecard&&HljtClassschedulecard.size()>0) {
			//String weeks=this.tetswork();
			for (int j = 0; j < 7; j++) {
				if(j==0) {
					
					List<Curriculum> Curriculumlist=new ArrayList<>();
					for (int i = 0; i < HljtClassschedulecard.size(); i++) {
						Curriculum Curriculum=new Curriculum();
						String Monday=HljtClassschedulecard.get(i).getMonday();
						if(null!=Monday&&Monday.length()>0) {
							String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, HljtClassschedulecard.get(i).getMonday());
							//设置第及节课
							Curriculum.setSection(i+1);
							Curriculum.setTeacherName(teachername);
							//设置科目
							Curriculum.setSubject(HljtClassschedulecard.get(i).getMonday());
							
							//设置第1.2.3。。。节开始时间
							Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
							//设置第1.2.3。。。节结束时间
							Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
							Curriculumlist.add(i, Curriculum);
						}else {
							Curriculumlist.add(i, Curriculum);
						}
					}
					mapCurriculum.put("Monday", Curriculumlist);
				}else if(j==1) {
					List<Curriculum> Curriculumlist=new ArrayList<>();
					for (int i = 0; i < HljtClassschedulecard.size(); i++) {
						Curriculum Curriculum=new Curriculum();
						String Tuesday=HljtClassschedulecard.get(i).getTuesday();
						if(null!=Tuesday&&Tuesday.length()>0) {
							String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, HljtClassschedulecard.get(i).getTuesday());
							//设置第及节课
							Curriculum.setSection(i+1);
							Curriculum.setTeacherName(teachername);
							//设置科目
							Curriculum.setSubject(HljtClassschedulecard.get(i).getTuesday());
							//设置第1.2.3。。。节开始时间
							Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
							//设置第1.2.3。。。节结束时间
							Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
							Curriculumlist.add(i, Curriculum);
						}else {
							Curriculumlist.add(i, Curriculum);
						}
					}
					mapCurriculum.put("Tuesday", Curriculumlist);
				}else if(j==2) {
					List<Curriculum> Curriculumlist=new ArrayList<>();
					for (int i = 0; i < HljtClassschedulecard.size(); i++) {
						Curriculum Curriculum=new Curriculum();
						String Wednesday=HljtClassschedulecard.get(i).getWednesday();
						if(null!=Wednesday&&Wednesday.length()>0) {
							String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, HljtClassschedulecard.get(i).getWednesday());
							//设置第及节课
							Curriculum.setSection(i+1);
							Curriculum.setTeacherName(teachername);
							//设置科目
							Curriculum.setSubject(HljtClassschedulecard.get(i).getWednesday());
							//设置第1.2.3。。。节开始时间
							Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
							//设置第1.2.3。。。节结束时间
							Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
							Curriculumlist.add(i, Curriculum);
						}else {
							Curriculumlist.add(i, Curriculum);
						}
					}
					mapCurriculum.put("Wednesday", Curriculumlist);
				}else if(j==3) {
					List<Curriculum> Curriculumlist=new ArrayList<>();
					for (int i = 0; i < HljtClassschedulecard.size(); i++) {
						Curriculum Curriculum=new Curriculum();
						String Thursday=HljtClassschedulecard.get(i).getThursday();
						if(null!=Thursday&&Thursday.length()>0) {
							String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, HljtClassschedulecard.get(i).getThursday());
						
							//设置第及节课
							Curriculum.setSection(i+1);
							Curriculum.setTeacherName(teachername);
							//设置科目
							Curriculum.setSubject(HljtClassschedulecard.get(i).getThursday());
							//设置第1.2.3。。。节开始时间
							Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
							//设置第1.2.3。。。节结束时间
							Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
							Curriculumlist.add(i, Curriculum);
						}else {
							Curriculumlist.add(i, Curriculum);
						}
					}
					mapCurriculum.put("Thursday", Curriculumlist);
				}else if(j==4) {
					List<Curriculum> Curriculumlist=new ArrayList<>();
					for (int i = 0; i < HljtClassschedulecard.size(); i++) {
						Curriculum Curriculum=new Curriculum();
							String Friday=HljtClassschedulecard.get(i).getFriday();
							if(null!=Friday&&Friday.length()>0) {
								String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, Friday);
							//	设置第及节课
								Curriculum.setSection(i+1);
								Curriculum.setTeacherName(teachername);
								//设置科目
								Curriculum.setSubject(Friday);
								//设置第1.2.3。。。节开始时间              
								Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
								//设置第1.2.3。。。节结束时间
								Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
								Curriculumlist.add(i, Curriculum);
							}else {
								Curriculumlist.add(i, Curriculum);
							}
						
					}
					mapCurriculum.put("Friday", Curriculumlist);
				}else if(j==5) {
					List<Curriculum> Curriculumlist=new ArrayList<>();
						for (int i = 0; i < HljtClassschedulecard.size(); i++) {
							String Saturday=HljtClassschedulecard.get(i).getSaturday();
							Curriculum Curriculum=new Curriculum();
							if(null!=Saturday&&Saturday.length()>0) {
								String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, HljtClassschedulecard.get(i).getSaturday());
								
								//设置第及节课
								Curriculum.setSection(i+1);
								Curriculum.setTeacherName(teachername);
								//设置科目
								Curriculum.setSubject(HljtClassschedulecard.get(i).getSaturday());
								//设置第1.2.3。。。节开始时间
								Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
								//设置第1.2.3。。。节结束时间
								Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
								Curriculumlist.add(i, Curriculum);
							}else {
								Curriculumlist.add(i, Curriculum);
							}
						}
						mapCurriculum.put("Saturday", Curriculumlist);
				}else if(j==6) {
					
					List<Curriculum> Curriculumlist=new ArrayList<>();
					for (int i = 0; i < HljtClassschedulecard.size(); i++) {
						String Sunday=HljtClassschedulecard.get(i).getSunday();
						Curriculum Curriculum=new Curriculum();
						if(null!=Sunday&&Sunday.length()>0) {
							String teachername=hljtTeacherclassServiceImpl.selsectTeacherName(classId, HljtClassschedulecard.get(i).getSunday());
								//设置第及节课
								Curriculum.setSection(i+1);
								Curriculum.setTeacherName(teachername);
								//设置科目
								Curriculum.setSubject(HljtClassschedulecard.get(i).getSunday());
								//设置第1.2.3。。。节开始时间
								Curriculum.setStartTime(HljtGradetime.get(i).getStarttime());
								//设置第1.2.3。。。节结束时间
								Curriculum.setEndTime(HljtGradetime.get(i).getEndtime());
								Curriculumlist.add(i, Curriculum);
						}else {
							Curriculumlist.add(i, Curriculum);
						}
					}
					mapCurriculum.put("Sunday", Curriculumlist);
				}
			}

			return mapCurriculum;	
		}else {
			
			return mapCurriculum;
			
		}
		
	}
	
	//获取当前是星期几
//	public static String tetswork() {
//		Date date = new Date();  
//		SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");  
//		String currSun = dateFm.format(date);  
//		return currSun;
//	}
	
	/**
	 * 获取本电脑ip
	 * @return
	 * @throws UnknownHostException
	 */
//	public static String getIp() throws UnknownHostException {
//		InetAddress addr = InetAddress.getLocalHost();  
//        String ip=addr.getHostAddress().toString();
//		return ip;
//	}
	 public static final String getIp() throws Exception {
	        String ipString = "";
	        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
	        InetAddress ip = null;
	        while (allNetInterfaces.hasMoreElements()) {
	            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
	            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
	            while (addresses.hasMoreElements()) {
	                ip = (InetAddress) addresses.nextElement();
	                if (ip != null && ip instanceof Inet4Address && !ip.getHostAddress().equals("127.0.0.1")) {
	                    return ip.getHostAddress();
	                }
	            }
	        }
	        return ipString;
	    }
	 

	 
}
